package com.czk.springboot.reactive.webflux;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-05-22
 * @Description:
 * @Version: 1.0
 */
public class OriginalWebFluxApplication {
    public static void main(String[] args) throws IOException {
        // 1 创建一个能处理http请求的处理器
        HttpHandler httpHandler = ((request, response) -> {
            System.out.println("请求进来了");
            // 响应数据缓冲区
            DataBufferFactory factory = response.bufferFactory();
            DataBuffer buffer = factory.wrap((request.getURI() + "hello").getBytes());
            // 响应结束
           // Mono<Void> voidMono = response.setComplete();
            return response.writeWith(Mono.just(buffer));
        });
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
        // 启动一个服务器
        HttpServer.create()
                .host("localhost")
                .port(8080)
                .handle(adapter)
                .bindNow();
        System.out.println("服务器启动完成");
        System.in.read();
        System.out.println("服务器停止");
    }
}
